﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using CERT.Services.DAL;
using CERT.Services;
using System.Web.Mvc;


namespace CERT.Controllers
{
    public class TeamDetailController : ApiController
    {
       private INETServices Service;


       public TeamDetailController()
       {
           Service = new NETServices();

       }

       public TeamDetailController(INETServices service)
        {
            Service = service;

        }

        

        // GET api/Team/5
       [ValidateAntiForgeryToken]
       [System.Web.Mvc.Authorize]
        public Team GetTeam(int id)
        {
            if (id == 0)
            {
                var newteam = new Team();
                var pteam = new People_TeamMember();
              //  var incident = new Incident();
                var person = new Person();
                pteam.Person = person;
                newteam.People_TeamMember.Add(pteam);
             //   newteam.Incidents.Add(incident);
                return newteam;
            }

            Team team = Service.GetSingleTeam(a => a.Id == id);
            if (team == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            return team;
        }

        // PUT api/Team/5
       [ValidateAntiForgeryToken]
       [System.Web.Mvc.Authorize]
        public HttpResponseMessage PutTeam(int id, People_TeamMember team)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

                var tm = Service.GetSingleTeam(a => a.Id == team.TeamId);
                team.Team = tm;


            try
            {
                Service.UpdateTeamMember(team, id);
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }
            catch (SystemException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex);
            }
            return Request.CreateResponse(HttpStatusCode.OK);
        }

       //  POST api/Team
       //[ValidateAntiForgeryToken]
       //[System.Web.Mvc.Authorize]
       // public HttpResponseMessage PostTeam(Team team)
       // {
       //     if (!ModelState.IsValid)
       //     {
       //         return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
       //     }
       //     try
       //     {
       //         Service.AddTeam(team);
       //     }
       //     catch (DbUpdateConcurrencyException ex)
       //     {
       //         return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
       //     }
       //     catch (SystemException ex)
       //     {
       //         return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex);
       //     }
       //     var response = Request.CreateResponse(HttpStatusCode.Created, team);
       //         response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = team.Id }));
       //         return response;
           
       // }



       // POST api/Team
       [ValidateAntiForgeryToken]
       [System.Web.Mvc.Authorize]
       public HttpResponseMessage PostMember(Team teamMember)
       {
           if (!ModelState.IsValid)
           {
               return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
           }
           try
           {

               var tm = teamMember.People_TeamMember.First();
               Service.AddTeamMember(tm);
           }
           catch (DbUpdateConcurrencyException ex)
           {
               return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
           }
           catch (SystemException ex)
           {
               return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex);
           }
           var response = Request.CreateResponse(HttpStatusCode.Created, teamMember);
           response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = teamMember.Id }));
           return response;

       }

        // DELETE api/Team/5
       [ValidateAntiForgeryToken]
       [System.Web.Mvc.Authorize]
        public System.Net.Http.HttpResponseMessage DeleteTeam(int id)
        {
           
            if (id == 0)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }
            
          

            try
            {
                Service.DeleteTeamMember(id);
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK, id);
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }
    }
}